package com.xhwl.centiotdata.service.integrateconfig;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationPropertiesDTO;
import com.xhwl.centiotdata.service.cent.IParameterConfigService;
import com.xhwl.common.enums.CategoryEnum;
import com.xhwl.common.pojo.cent.ParameterConfig;
import com.xhwl.common.pojo.criteria.ParameterConfigCriteria;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author： lianghz
 * @Description：
 * @Date 2021/12/16/ 15:12
 */
@Component
@Slf4j
public class PedestrianIntegrationConfig extends IntegrationConfig<Object, Map<Integer, List<PedestrianIntegrationConfigDTO>>> {

    private Map<Integer, List<PedestrianIntegrationConfigDTO>> pedestrianIntegrationConfigMap = new HashMap<>();

    /**
     * 配置暂且写到人行配置
     */
    private static Set<PedestrianIntegrationConfigDTO> enterpriseConfigList = new HashSet<>();

    @Resource
    private IParameterConfigService iParameterConfigService;

    @Override
    public Map<Integer, List<PedestrianIntegrationConfigDTO>> getIntegrationConfig(Object o) {
        return pedestrianIntegrationConfigMap;
    }

    /**
     * 配置变更时，重新赋值map
     * @param event
     */
    @EventListener
    public void listenIntegrationConfig(IntegrationConfigEvent event) {
        this.handlePedestrianIntegrationConfig();
        this.handleEnterpriseConfig();
    }

    public static List<PedestrianIntegrationConfigDTO> getEnterpriseConfig(Integer enterpriseId) {
        List<PedestrianIntegrationConfigDTO> pedestrianIntegrationConfigDTOSList = new ArrayList<>();
        List<Integer> productList = new ArrayList<>();
        enterpriseConfigList.forEach(s->{
            if(enterpriseId.equals(s.getEnterpriseId()) &&(CollectionUtils.isEmpty(productList) || !productList.contains(s.getProductId()))){
                productList.add(s.getProductId());
                pedestrianIntegrationConfigDTOSList.add(s);
            }
        });
        log.info("企业ID:" + enterpriseId + "配置信息" + enterpriseConfigList+" 人行列表："+pedestrianIntegrationConfigDTOSList);
        return pedestrianIntegrationConfigDTOSList;//enterpriseConfigList.stream().filter(t -> t.getEnterpriseId().equals(enterpriseId)).collect(Collectors.toList());
    }

    private void handleEnterpriseConfig() {
        log.info("----------------开始获取参数配置----------------");
        ParameterConfigCriteria criteria = new ParameterConfigCriteria();
        criteria.withKeywords("ENTERPRISE_SYNC_CONFIG");
        Page<ParameterConfig> page = iParameterConfigService.findByPage(criteria, new Page());
        List<ParameterConfig> records = page.getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            log.info("获取到的企业同步配置:{}", JSONObject.toJSONString(records));
            records.forEach(record -> {
                PedestrianIntegrationConfigDTO pedestrianIntegrationConfigDTO = JSONObject.parseObject(record.getValue(), PedestrianIntegrationConfigDTO.class);
                enterpriseConfigList.add(pedestrianIntegrationConfigDTO);
            });
        }

    }


    private void handlePedestrianIntegrationConfig() {
        pedestrianIntegrationConfigMap = IntegrationConfig.integrationConfigList.stream()
                .filter(deviceProjSupplierCategory ->
                        CategoryEnum.DOOR.getId().equals(deviceProjSupplierCategory.getProdCategoryId()) && deviceProjSupplierCategory.getProperties() != null)
                .map(deviceProjSupplierCategory -> {
                    JSONObject properties = deviceProjSupplierCategory.getProperties();
                    PedestrianIntegrationPropertiesDTO propertiesDTO = JSON.toJavaObject(properties, PedestrianIntegrationPropertiesDTO.class);
                    if (!CollectionUtils.isEmpty(propertiesDTO.getMethodIdList())) {
                        return propertiesDTO.getMethodIdList().stream().map(methodId -> {
                            PedestrianIntegrationConfigDTO configDTO = new PedestrianIntegrationConfigDTO();
                            BeanUtils.copyProperties(propertiesDTO, configDTO);
                            configDTO.setMethodId(methodId);
                            configDTO.setProjectId(deviceProjSupplierCategory.getProjectId());
                            configDTO.setProductId(deviceProjSupplierCategory.getProductId());
                            return configDTO;
                        }).collect(Collectors.toList());
                    } else {
                        return null;
                    }
                })
                .filter(configDTOList -> !CollectionUtils.isEmpty(configDTOList))
                .flatMap(configDTOList -> configDTOList.stream())
                .filter(pedestrianIntegrationConfigDTO -> pedestrianIntegrationConfigDTO != null && pedestrianIntegrationConfigDTO.getMethodId() != null)
                .collect(Collectors.groupingBy(PedestrianIntegrationConfigDTO::getMethodId));
    }

}
